﻿using Abp.Application.Services;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using AbpVueDemo.SystemBase.DictionaryTableManager;
using AbpVueDemo.SystemBase.DictionaryTypeTableManager.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbpVueDemo.SystemBase.DictionaryTypeTableManager
{

    /// <summary>
    /// 字典类别表服务  lzh
    /// 继承AsyncCrudAppService 增删改查实现类
    /// DictionaryTypeTable  数据模型与数据库一致
    ///   DictionaryTableTypeInputDto 表示字典表的数据传输对象 ,用于返回数据
    ///   string 表示操作数据类的主键类型
    ///  PageDictionaryTableTypeRequestDto 分页请求对象 
    ///  DictionaryTableTypeCreateDto 字典创建对象 
    ///  DictionaryTableTypeUpdateDto 字典修改对象 
    /// </summary>
    [AbpAuthorize]
    public  class DictionaryTableTypeService: AsyncCrudAppService<DictionaryTypeTable, DictionaryTableTypeInputDto, string, PageDictionaryTableTypeRequestDto, DictionaryTableTypeCreateDto, DictionaryTableTypeUpdateDto>, IDictionaryTableTypeService
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository">注入仓储层(类似于以前的上下文)</param>
        public DictionaryTableTypeService(
        IRepository<DictionaryTypeTable, string> repository) : base(repository)
        {
     
        }
        /// <summary>
        /// 自定义分页条件查询, 需要重写父类的CreateFilteredQuery方法,让其进行自定义条件筛选
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns></returns>
        protected override IQueryable<DictionaryTypeTable> CreateFilteredQuery(PageDictionaryTableTypeRequestDto input)
        {
            return base.CreateFilteredQuery(input)
                .WhereIf(!string.IsNullOrEmpty(input.dt_type_name), c => c.dt_type_name.Contains(input.dt_type_name))
                .WhereIf(!string.IsNullOrEmpty(input.Id), c => c.Id.Contains(input.Id));
        }

        /// <summary>
        /// 验证字典Key是否唯一
        /// </summary>
        /// <param name="id">字典Key</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        [Microsoft.AspNetCore.Mvc.HttpGet]
        public async Task<bool> ValidatorId(string id)
        {
            if (string.IsNullOrEmpty(id)) throw new Exception("id参数输入错误");
            var dctTypeInfo = await Repository.FirstOrDefaultAsync(c=>c.Id==id);
            if (dctTypeInfo == null) return true;
            return false;
        }




    }
}
